<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="specialList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="类型" align="center" prop="averageType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sl_special_type" :value="scope.row.averageType"/>
        </template>
      </el-table-column>
      <el-table-column label="结算系数" align="center" prop="ssScale" />
      <el-table-column label="工资系数" align="center" prop="ssRatio" />
      <el-table-column label="是否参与生产" align="center" prop="isProduce">
        <template slot-scope="scope" >
          <dict-tag :options="dict.type.sl_special_produce" :value="scope.row.isProduce"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sl:special:edit']"
          >修改</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sl:special:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改特殊系数对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="16">
            <el-form-item label="用户昵称" prop="nickName">
              {{form.nickName}}
             <!-- <el-input v-model="form.nickName" placeholder="请选择用户" >
                <el-button slot="append" @click="handleUser2Select" icon="el-icon-search"></el-button>
              </el-input> -->
            </el-form-item>
            <!-- <UserSingleSelect ref="user2Select" @onSelected="onUser2Selected"></UserSingleSelect> -->
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="类型" prop="averageType">
              <el-select v-model="form.averageType" placeholder="请选择类型" @change="handleChangeaverAgeType">
                <el-option
                  v-for="dict in dict.type.sl_special_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8" v-if="form.averageType!=null && form.averageType!='4'">
            <el-form-item label="模块" prop="moduleId">
                <el-select v-model="form.moduleId" placeholder="请选择模块" @change="handleChangeModule" >
                  <el-option
                    v-for="item in moduleOptions"
                    :key="item.moduleId"
                    :label="item.moduleName"
                    :value="item.moduleId"
                    :disabled="item.status == 1"
                  ></el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="form.averageType!=null && (form.averageType=='2' || form.averageType=='3') ">
            <el-form-item label="车间" prop="workshopId">
                <el-select v-model="form.workshopId" @change="handleChangeWorkshop" placeholder="请选择车间">
                  <el-option
                    v-for="item in workshopOptions"
                    :key="item.workshopId"
                    :label="item.workshopName"
                    :value="item.workshopId"
                    :disabled="item.status == 1"
                  ></el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="form.averageType!=null && form.averageType=='3'">
            <el-form-item label="工序" prop="gprocessId">
                <el-select v-model="form.gprocessId" placeholder="请选择工序">
                  <el-option
                    v-for="item in gprocessOptions"
                    :key="item.gprocessId"
                    :label="item.gprocessName"
                    :value="item.gprocessId"
                    :disabled="item.status == 1"
                  ></el-option>
                </el-select>
            </el-form-item>
          </el-col>
        </el-row>
         <el-form-item label="结算系数" prop="ssScale">
            <el-input v-model="form.ssScale" class="def-input-number" type="number" placeholder="请输入结算系数" />
          </el-form-item>
        <el-row>
          <el-col :span="16">
            <el-form-item label="工资系数" prop="ssRatio">
              <el-input v-model="form.ssRatio" class="def-input-number" type="number" placeholder="请输入工资系数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="是否参与生产" prop="isProduce">
          <el-radio-group v-model="form.isProduce">
            <el-radio
              v-for="dict in dict.type.sl_special_produce"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSpecial, getSpecial,getUpdateSpecial, delSpecial, addSpecial, updateSpecial } from "@/api/sl/special";
import UserSingleSelect from "@/components/userSelect/single.vue"
import { getWorkshopListByModuleId } from "@/api/sl/workshop";
import { getGprocessListByWorkshopId } from "@/api/sl/gprocess";

export default {
  name: "Special",
  components: {UserSingleSelect},
  dicts: ['sl_special_type',"sl_special_produce"],
  data() {
    let watermarkContentVaild = (rule, value, callback) => {
      if (value == null || value === "") {
        callback();
      } else {
        let length = value.length
        if (length > 7) {
          callback(new Error("输入不能超过7位"))
        } else {
          callback();
        }
      }
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 特殊系数表格数据
      specialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        nickName: null,
        averageType: null,
        moduleId: null,
        workshopId: null,
        gprocessId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "用户不能为空", trigger: "blur" }
        ],
        averageType: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
         ssScale: [
          { required: true, message: "结算系数不能为空", trigger: "blur" },
           {validator: watermarkContentVaild, trigger: 'blur'}
        ],
        ssRatio: [
          { required: true, message: "工资系数不能为空", trigger: "blur" },
          {validator: watermarkContentVaild, trigger: 'blur'}
        ],
        moduleId: [
          { required: true, message: "模块不能为空", trigger: "blur" }
        ],
        workshopId: [
          { required: true, message: "车间不能为空", trigger: "blur" }
        ],
        gprocessId: [
          { required: true, message: "工序不能为空", trigger: "blur" }
        ],
        isProduce: [
          { required: true, message: "是否参与生产不能为空", trigger: "blur" }
        ],

      },
      moduleOptions: [],
      workshopOptions: [],
      gprocessOptions: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询特殊系数列表 */
    getList() {
      this.loading = true;
      listSpecial(this.queryParams).then(response => {
        // console.log(response.rows)
        this.specialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ssId: null,
        userName: null,
        nickName: null,
        averageType: null,
        moduleId: null,
        workshopId: null,
        gprocessId: null,
        ssScale: null,
        ssRatio: null,
        ssSort: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        isProduce : "1"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ssId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getSpecial().then(response => {
        this.moduleOptions = response.modules;
        this.open = true;
        this.title = "添加特殊系数";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userName = row.userName
      getUpdateSpecial(userName).then(response => {
        this.form = response.data;
        this.moduleOptions = response.modules;
        this.workshopOptions = response.workshops;
        this.gprocessOptions = response.gprocesss;
        this.open = true;
        this.title = "修改特殊系数";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ssId != null) {
            updateSpecial(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSpecial(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ssIds = row.ssId || this.ids;
      this.$modal.confirm('是否确认删除特殊系数编号为"' + ssIds + '"的数据项？').then(function() {
        return delSpecial(ssIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sl/special/export', {
        ...this.queryParams
      }, `special_${new Date().getTime()}.xlsx`)
    },
    //点击人员选择按钮
    handleUser2Select(){
        this.$refs.user2Select.showFlag = true;
    },
    //人员选择返回
    onUser2Selected(row){
        this.form.userName = row.userName;
        this.form.nickName = row.nickName;
    },
    //模块变更查询车间
    handleChangeModule(value){
      //清空车间
      this.form.workshopId = null;
      //清空大工序
      this.form.gprocessId = null;
      getWorkshopListByModuleId(value).then(response => {
        this.workshopOptions = response.data;
      });
    },
    //车间变更查询大工序
    handleChangeWorkshop(value){
      //清空车间
      this.form.gprocessId = null;
      getGprocessListByWorkshopId(value).then(response => {
        this.gprocessOptions = response.data;
      });
    },
    handleChangeaverAgeType(value){

    }
  }
};
</script>
